import json
import sys
import os

sys.path.append(os.path.dirname(os.path.abspath(__file__)))

user_info_path = 'influxdbHome/user_info/'
sql_file_name = '/sql.txt'
json_file_name = '/conf.json'
init_json = '{"IP": "172.26.173.244", "PID": "f51bc029-6211-4b4a-85c3-45ec65f3f761", "IDS": "EIFE_AL1_503_PM5350_2.WHr_I", "StartTime": "2023-01-19", "Days": "380", "Hours": "0", "Minutes": "15", "Seconds": "0", "DataType": "float", "BaseNumber": "80", "Range": "100", "Step": "0.8", "SubmitType": "", "port": 8086, "influxDB_name": "admin", "influxDB_table": "scada", "ifAdd": 1, "sql": "SELECT float,integer,boolean,string FROM \'f51bc029-6211-4b4a-85c3-45ec65f3f761\' WHERE \'Path\' = \'GW01_MIC50X_2.WHr\' ORDER BY time desc limit 3", "file": "searchResult.txt", "ModsimIP": "172.26.172.201", "ModsimPort": "502", "ModsimAddress": "32001", "ModsimType": "UINT16", "ModsimNumber": "1"}'


def write_json_file(file_path,data):
    with open(file_path, 'w') as f:
        json.dump(data, f)

#获取表单参数，格式为json
def get_json_data(user_name):
    file_path = create_path_file(user_name, json_file_name)
    with open(file_path, 'r') as f:
        data = json.load(f)
    return data


def modify_json_data(user_name,**default):
    read_data = get_json_data(user_name)
    file_path = create_path_file(user_name,json_file_name)
    # 修改默认值
    for key, value in default.items():
        read_data[key] = value
    write_json_file(file_path,read_data)


# 创建用户查询结果的路径和文件
def create_path_file(user_name, file_name):
    path = user_info_path + user_name
    file_path = path + file_name
    # 如果路径不存在就创建一个
    create_unexist_path(path)
    # 如果文件不存在就创建一个
    create_unexist_file(file_path)
    return file_path


def create_unexist_path(path):
    if not os.path.exists(path):
        os.makedirs(path)


def create_unexist_file(file_path):
    print(file_path)
    if not os.path.isfile(file_path):
        # 如果文件不存在，则创建它
        open(file_path, 'w').close()
        if 'txt' in file_path:
            with open(file_path, 'w') as file:
                file.write(
                    'SELECT min(float) FROM "f51bc029-6211-4b4a-85c3-45ec65f3f761" WHERE Path =\'GW01_PM5350_1.Ia2345\' group by time(1h),Path\n')
        elif 'json' in file_path:
            with open(file_path, 'w') as file:
                #初始化json文件
                file.write(init_json)

def get_json_sql(user_name):
    file_path = create_path_file(user_name, sql_file_name)
    with open(file_path, 'r') as f:
        sqls = f.readlines()
    return sqls


def recordsql(sql, user_name):
    res = True
    file_path = create_path_file(user_name, sql_file_name)
    with open(file_path, 'r') as f:
        sqls = f.readlines()
        if len(sqls) > 0:
            for existsql in sqls:
                if sql in existsql:
                    res = False
                else:
                    continue
    return res


def updateSqlList(sql, user_name):
    file_path = create_path_file(user_name, sql_file_name)
    with open(file_path, 'r') as f:
        lines = f.readlines()
    if len(lines) > 5:
        print(len(lines))
        del lines[0]
        with open(file_path, 'w') as f:
            for line in lines:
                f.write(line)
            f.write(sql)
            f.write("\n")
    else:
        with open(file_path, 'a') as f:
            f.write(sql)
            f.write("\n")
